//
//  XqimageViewController.m
//  Koala_Fm
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "XqimageViewController.h"
#import "XqEnterTableViewCell.h"
#import "AFNetBlock.h"
#import <UIImageView+WebCache.h>
#import "XqEntModel.h"
#import "ImageModel.h"
#import "ImageTableViewCell.h"
@interface XqimageViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, retain)UIView  *header;
@property (nonatomic, retain)UILabel *lable;
@property (nonatomic, retain)UIImageView *headImage;
@property (nonatomic, retain)UILabel *timeLable;
@property (nonatomic, retain)UILabel *nameLable;
@property (nonatomic, retain)UILabel *rmLable;
@property (nonatomic, retain)UIImageView *image;

@end

@implementation XqimageViewController
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_header release];
    [_lable release];
    [_headImage release];
    [_timeLable release];
    [_nameLable release];
    [_rmLable release];
    [_image release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49 ) style:UITableViewStylePlain];
//    _tableView.backgroundColor = [UIColor redColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    // 重用池
    [_tableView registerClass:[XqEnterTableViewCell class] forCellReuseIdentifier:@"cell"];
//    NSLog(@"%@", _mo.imageId);
    // 数据获取
    NSString *str = @"http://api.budejie.com/api/api_open.php?market=tencentyingyongbao&udid=862966024235029&a=dataList&appname=baisibudejie&c=comment&os=4.2.2&client=android&userID=&page=1&per=20&visiting=&data_id=";
    NSString *strOne = @"&lastcid=&hot=1&mac=8c%3Abe%3Abe%3A5c%3A9f%3A66&ver=6.3.0";
    NSString *strTow =[NSString stringWithFormat:@"%@%@%@", str, _mo.imageId, strOne];
    
    [AFNetBlock GetDataWithURL:strTow block:^(id block) {
        
        
        
                    NSArray *array = block[@"data"];
        self.dataArr = [XqEntModel modelHandlerWithArray:array];
        //   NSLog(@"%@", _dateArr);
        
        // 刷新页面
        
        [_tableView reloadData];
        
    }];

}
#pragma mark - tableView协议设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XqEnterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    XqEntModel *m = _dataArr[indexPath.row];
    cell.commLable.text = m.content;
    
    cell.nameLable.text = m.username;
    cell.upLable.text = m.like_count;
    [cell.avatarImage sd_setImageWithURL:[NSURL URLWithString:m.profile_image]];
    cell.imagOne.image = [UIImage imageNamed:@"zan"];
    if ([m.sex isEqualToString:@"f"]) {
        cell.genderImage.image =[UIImage imageNamed:@"nv"];
    }else{
        cell.genderImage.image = [UIImage imageNamed:@"nan"];
    }
     /** 头部区域 **/
    [self tobqy];
    
    
    return cell;
}
//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XqEntModel *m = _dataArr[indexPath.row];
    CGFloat h = [XqEnterTableViewCell heightWithText:m.content];
    
    return 100 +(h - 5);
}


#pragma mark - 头部区域设置
- (void)tobqy{
    //调用文字高度
    CGFloat h =  [ImageTableViewCell heightWithText:_mo.text];
    
    //判断是否是动图
    CGFloat dh;
    // 添加高度
    if (_mo.big != nil) {
        dh = [ImageTableViewCell heightWithHeight:_mo.height width:_mo.width];
    }
    else{
        dh = [ImageTableViewCell heightWithHeight:_mo.heightD  width:_mo.widthD];
    }

    //     添加头部区域
    self.header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60 + h + dh + 40)];
    //    _header.backgroundColor = [UIColor redColor];
    _tableView.tableHeaderView = _header;
    // 名字
    self.nameLable = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, 150, 20)];
    //    self.nameLable.backgroundColor = [UIColor redColor];
    self.nameLable.text = _mo.name;
    [_header addSubview:_nameLable];
    [_header release];
    // 时间
    self.timeLable = [[UILabel alloc]initWithFrame:CGRectMake(60, 25, 150, 20)];
    //    self.timeLable.backgroundColor = [UIColor blackColor];
    self.timeLable.text =_mo.passtime;
    [_header addSubview:_timeLable];
    [_timeLable release];
    // 头像
    self.headImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 50, 50)];
    // 切圆
    _headImage.layer.cornerRadius = 25;
    _headImage.clipsToBounds = YES;
    //    self.headImage.backgroundColor = [UIColor yellowColor];
    [_headImage sd_setImageWithURL:[NSURL URLWithString:_mo.header]];
    [_header addSubview:_headImage];
    [_headImage release];
    // 图片
    self.image = [[UIImageView alloc]initWithFrame:CGRectMake(5, 60 + h, self.view.frame.size.width, dh)];
//    self.imag.backgroundColor = [UIColor yellowColor];
    // 判断是否是动图
    if (_mo.images != nil) {
        [_image sd_setImageWithURL:[NSURL URLWithString:_mo.images]];
     
    }else{
        [_image sd_setImageWithURL:[NSURL URLWithString:_mo.big]];
    
    }
    [_header addSubview:_image];
    [_image release];
    
    
    // 文字
    self.lable = [[UILabel alloc]initWithFrame:CGRectMake(5, 60, self.view.frame.size.width, h )];
    //    self.lable.backgroundColor = [UIColor yellowColor];
    self.lable.numberOfLines = 0;
    [_header addSubview:_lable];
    self.lable.text =_mo.text;
    [_lable release];
    //
    self.rmLable = [[UILabel alloc]initWithFrame:CGRectMake(5, 60 + h + dh + 20, 100, 20)];
    self.rmLable.backgroundColor = [UIColor grayColor];
    self.rmLable.text = @"热门评论";
    [_header addSubview:_rmLable];
    [_rmLable release];
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
